Overview of Dialog in JavaScript

Popup dialog functions

JavaScript provides three popup dialog functions. These functions allow you to alert the user, solicit confirmation, and request data.

alert(aString) Displays an Alert box with the message and an OK button, e.g., alert("Cancelled by User Request"). Used to alert users of important conditions.
confirm(aString) Displays a Confirm dialog box with the message and both OK and CANCEL buttons, e.g., confirm("Really Leave?"). Used to prompt user for a decision.
prompt(message, default) Displays a Prompt dialog box with the message, a text entry input and both OK and CANCEL buttons, e.g., ans=prompt("Message", "TheDefaultResponse");
if (!ans) alert("You pressed cancel.")
else if alert("You said "+ans)
.


Window properties

Modify these properties to alter the window's status bar. Set these properties from within your JavaScript scripts to update the text displayed in the status bar.

defaultStatus Change the default value of the status bar text, e.g., window.defaultStatus="Pick an option".
status Change the immediate value of the status bar text, e.g., window.status="This link leads to more information". This value will disappear at the next onMouseOver event, usually the time that the mouse next moves.


Creating documents

JavaScript provides a number of document writing, style, and link functions. These functions can only be used at document creation time. Try to use them after a document has been created and you'll spawn a new window or overwrite your current document instead. Create content with document.open(), followed by a series of writes and style/link calls, and finished with (the unintuitive) document.close(). This closes the writing process, not the window.

For example,

document.open()
document.write('<H1>Hello World!</H1>')
document.close()
Document Creation
open() Reset the document and prepare to write, e.g., document.open().
close() Finish writing to the document and display it, e.g., document.close().
Writing
write(aString) Adds the string to the new document. The string can include HTML directives, e.g., document.write('<H1>Hello World!</H1>'). Used to build the contents of a window.
writeln(aString) Same as write, but carriage return appended at the end. Useful for preformatted text (<PRE>), e.g., document.writeln('<H1>Hello World!</H1>').
Formats
big() Create a string which uses a big font, e.g., document.write('Hello World'.big()) is the same as document.write('<BIG>Hello World</BIG>').
blink() Create a blinking string, e.g., document.write('Hello World'.blink()) is the same as document.write('<BLINK>Hello World</BLINK>').
bold() Create a string with a bold font, e.g., document.write('Hello World'.bold()) is the same as document.write('<B>Hello World</B>').
fixed() Create a string with a fixed (non-proportional) font, e.g., document.write('Hello World'.fixed()) is the same as document.write('<TT>Hello World</TT>').
fontcolor(aColor) Create a string with a particular color, e.g., document.write('Hello World'.fontcolor(aColor)) is the same as document.write('<FONT COLOR=aColor>Hello World</FONT>').
fontsize(aSize) Create a string with a particular fontsize, e.g., document.write('Hello World'.fontsize(aSize)) is the same as document.write('<FONT SIZE=aSize>Hello World</FONT>').
italics() Create a string which uses an italicized font, e.g., document.write('Hello World'.italics()) is the same as document.write('<I>Hello World</I>').
small() Create a string which uses a small font, e.g., document.write('Hello World'.small()) is the same as document.write('<SMALL>Hello World</SMALL>').
strike() Create a string with struck-out text, e.g., document.write('Hello World'.strike()) is the same as document.write('<STRIKE>Hello World</STRIKE>').
sub() Create a string which displays as a subscript, e.g., document.write('Hello World'.sub()) is the same as document.write('<SUB>Hello World</SUB>').
sup() Create a string which displays as a superscript, e.g., document.write('Hello World'.sup()) is the same as document.write('<SUP>Hello World</SUP>').
Anchors and links
anchor(aName) Create an anchor, e.g., document.write('Hello World'.anchor(aName)) is the same as document.write('<A NAME="aName">Hello World</A>').
link(anHref) Create a link, e.g., document.write('Hello World'.link(anHref)) is the same as document.write('<A HREF="anHref">Hello World</A>').
Copyright ©1998 by Charles River Media, All Rights Reserved